package cn.stylefeng.guns.sys.modular.system.controller;

import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.sys.core.util.DateUtil;
import cn.stylefeng.guns.sys.modular.system.entity.DsstoreBillDelivery;
import cn.stylefeng.guns.sys.modular.system.model.params.DsstoreBillDeliveryAuditParam;
import cn.stylefeng.guns.sys.modular.system.model.params.DsstoreBillDeliveryParam;
import cn.stylefeng.guns.sys.modular.system.model.params.DsstoreBillDeliveryVHideParam;
import cn.stylefeng.guns.sys.modular.system.service.DsstoreBillDeliveryAuditService;
import cn.stylefeng.guns.sys.modular.system.service.DsstoreBillDeliveryService;
import cn.stylefeng.guns.sys.modular.system.service.DsstoreBillDeliveryVHideService;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;

/**
 * 控制器
 *
 * @author jiangge
 * @Date 2021-07-12 10:58:49
 */
@Controller
@RequestMapping("/dsstoreBillDelivery")
public class DsstoreBillDeliveryController extends BaseController {

    private String PREFIX = "/modular/system/dsstoreBillDelivery";

    @Autowired
    private DsstoreBillDeliveryService dsstoreBillDeliveryService;
    @Autowired
    private DsstoreBillDeliveryAuditService dsstoreBillDeliveryAuditService;
    @Autowired
    private DsstoreBillDeliveryVHideService dsstoreBillDeliveryVHideService;

    /**
     * 跳转到主页面
     *
     * @author jiangge
     * @Date 2021-07-12
     */
    @RequestMapping("")
    public String index(Model model) {
        String dateStr = DateUtil.getLastMonthStr();
        model.addAttribute("dateStr", dateStr);
        return PREFIX + "/dsstoreBillDelivery.html";
    }

    /**
     * 新增页面
     *
     * @author jiangge
     * @Date 2021-07-12
     */
    @RequestMapping("/add")
    public String add() {
        return PREFIX + "/dsstoreBillDelivery_add.html";
    }

    /**
     * 编辑页面
     *
     * @author jiangge
     * @Date 2021-07-12
     */
    @RequestMapping("/edit")
    public String edit() {
        return PREFIX + "/dsstoreBillDelivery_edit.html";
    }

    /**
     * 新增接口
     *
     * @author jiangge
     * @Date 2021-07-12
     */
    @RequestMapping("/addItem")
    @ResponseBody
    public ResponseData addItem(DsstoreBillDeliveryParam dsstoreBillDeliveryParam) {
        this.dsstoreBillDeliveryService.add(dsstoreBillDeliveryParam);
        return ResponseData.success();
    }

    /**
     * 编辑接口
     *
     * @author jiangge
     * @Date 2021-07-12
     */
    @RequestMapping("/editItem")
    @ResponseBody
    public ResponseData editItem(DsstoreBillDeliveryParam dsstoreBillDeliveryParam) {
        this.dsstoreBillDeliveryService.update(dsstoreBillDeliveryParam);
        return ResponseData.success();
    }

    /**
     * 删除接口
     *
     * @author jiangge
     * @Date 2021-07-12
     */
    @RequestMapping("/delete")
    @ResponseBody
    public ResponseData delete(DsstoreBillDeliveryParam dsstoreBillDeliveryParam) {
        this.dsstoreBillDeliveryService.delete(dsstoreBillDeliveryParam);
        return ResponseData.success();
    }

    /**
     * 查看详情接口
     *
     * @author jiangge
     * @Date 2021-07-12
     */
    @RequestMapping("/detail")
    @ResponseBody
    public ResponseData detail(DsstoreBillDeliveryParam dsstoreBillDeliveryParam) {
        DsstoreBillDelivery detail = this.dsstoreBillDeliveryService.getById(dsstoreBillDeliveryParam.getStoreBillDtid());
        return ResponseData.success(detail);
    }

    /**
     * 查询列表
     *
     * @author jiangge
     * @Date 2021-07-12
     */
    @ResponseBody
    @RequestMapping("/list")
    public LayuiPageInfo list(DsstoreBillDeliveryParam dsstoreBillDeliveryParam) {
        return this.dsstoreBillDeliveryService.findPageBySpec(dsstoreBillDeliveryParam);
    }

    /**
     * 查询列表
     *
     * @author jiangge
     * @Date 2021-07-12
     */
    @ResponseBody
    @RequestMapping("/list2")
    public LayuiPageInfo list2(DsstoreBillDeliveryParam param) {
        //TODO-1 处理日期参数
        setYearAndMonth(param);
        //TODO--2 查询
        return this.dsstoreBillDeliveryService.findPageByParam(param);
    }

    /**
     * 把格式为 yyyy-mm的年月日期，赋值给年参数和月参数
     *
     * @param param
     */
    private void setYearAndMonth(DsstoreBillDeliveryParam param) {
        //参数年,月
        String year = "";
        String month = "";
        if (StringUtils.isNotBlank(param.getDateStr())) {
            year = param.getDateStr().substring(0, 4);
            month = param.getDateStr().substring(6, 7);
            param.setAPYear(Long.parseLong(year));
            param.setAPMonth(Integer.parseInt(month));
        }

        //处理itemCode
        if (StringUtils.isNotBlank(param.getItemCode())) {
            String[] strArray = param.getItemCode().split(",");
            List<String> itemCodesList = Arrays.asList(strArray);
            param.setItemCodeList(itemCodesList);
        }

    }

    //导出
    @RequestMapping("/listExport")
    public ResponseEntity<byte[]> exportExcel(HttpServletRequest request, HttpServletResponse response, DsstoreBillDeliveryParam param) {
        setYearAndMonth(param);
        return this.dsstoreBillDeliveryService.exportExcel(request, response, param);
    }

    /**
     * 审核:改变状态 0：未审核，1：已审核
     */
    @RequestMapping(value = "/audit", method = RequestMethod.POST)
    @ResponseBody
    public ResponseData adminAudit(DsstoreBillDeliveryAuditParam param) {
        ResponseData responseData = this.dsstoreBillDeliveryAuditService.audit(param);
        return responseData;
    }

    /**
     * 恢复数据
     */
    @RequestMapping(value = "/recover", method = RequestMethod.POST)
    @ResponseBody
    public ResponseData recover(DsstoreBillDeliveryAuditParam param) {
        DsstoreBillDeliveryVHideParam param1=new DsstoreBillDeliveryVHideParam();
        param1.setDeliveryId(param.getId());
        this.dsstoreBillDeliveryVHideService.delete(param1);
        return ResponseData.success();
    }

    /**
     * 发送店长，修改状态 从0到1,除了通过的都可以再次发送
     *
     * @author jiangge
     * @Date 2021-07-08
     */
    @RequestMapping("/batchAudit")
    @ResponseBody
    public ResponseData batchAudit(DsstoreBillDeliveryAuditParam param) {
        ResponseData responseData = this.dsstoreBillDeliveryAuditService.batchAudit(param);
        return responseData;
    }


    /**
     * 批量删除数据
     *
     * @author jiangge
     * @Date 2021-07-08
     */
    @RequestMapping("/batchDel")
    @ResponseBody
    public ResponseData batchDel(DsstoreBillDeliveryVHideParam param) {
        if(StringUtils.isNotBlank(param.getIds())){
            String[] split = param.getIds().split(",");

            List<String> idList = Arrays.asList(split);
            this.dsstoreBillDeliveryVHideService.addList(idList);
        }
        return ResponseData.success();
    }

    /**
     * 批量恢复数据
     *
     * @author jiangge
     * @Date 2021-07-08
     */
    @RequestMapping("/batchRecover")
    @ResponseBody
    public ResponseData batchRecover(DsstoreBillDeliveryVHideParam param) {
        if(StringUtils.isNotBlank(param.getIds())){
            String[] split = param.getIds().split(",");
            List<String> idList = Arrays.asList(split);
            this.dsstoreBillDeliveryVHideService.deleteList(idList);
        }
        return ResponseData.success();
    }
}


